package com.tiny.persistence.repository.impl;

import java.util.List;
import java.util.Map;

import javax.persistence.Query;

import org.apache.log4j.Logger;

import com.ai.web.cache.Constants;
import com.ai.web.cache.UserContext;
import com.tiny.persistence.domain.BgUser;
import com.tiny.persistence.repository.plus.BgUserRepoPlus;

/**
 * 后台用户(isWxUser=0)
 * 
 * @ClassName: BgBgUserRepoImpl
 * @Description: TODO
 * @author:
 * @date: 2014年3月28日
 * 
 */
public class BgUserRepoImpl extends BaseRepoImpl<BgUser> implements
		BgUserRepoPlus {

	private static Logger logger = Logger.getLogger(BgUserRepoImpl.class);

	public BgUser findByUsernameAndPassword(String username, String password) {

		logger.info("RepoImpl username=" + username + " pass=" + password);
		Query query = em
				.createQuery("from BgUser as au where au.username=:username and au.password=:password");
		query.setParameter("username", username);
		query.setParameter("password", password);
		@SuppressWarnings("unchecked")
		List<BgUser> list = query.getResultList();
		if (list != null && list.size() > 0) {
			return list.get(0);
		}
		return null;

	}

	@SuppressWarnings("unchecked")
	@Override
	public List<BgUser> getListByCondition(Map<String, Object> condition) {
		StringBuffer sqlquery = new StringBuffer();
		sqlquery.append("from BgUser as a where 1=1 and a.id>2 ");

		// 查询条件
		String username = null;
		String company = null;
		String mobilePhone = null;
		if (condition.get("username") != null) {
			username = String.valueOf(condition.get("username"));
		}
		if (condition.get("company") != null) {
			company = String.valueOf(condition.get("company"));
		}
		if (condition.get("mobilePhone") != null) {
			mobilePhone = String.valueOf(condition.get("mobilePhone"));
		}
		//
		if (username != null) {
			sqlquery.append(" and a.username like :username");
		}
		if (company != null) {
			sqlquery.append(" and a.company like :company");
		}
		if (mobilePhone != null) {
			sqlquery.append(" and a.mobilePhone like :mobilePhone");
		}

		Query query = em.createQuery(sqlquery.toString());
		if (username != null) {
			query.setParameter("username", "%" + username + "%");
		}
		if (company != null) {
			query.setParameter("company", "%" + company + "%");
		}
		if (mobilePhone != null) {
			query.setParameter("mobilePhone", "%" + mobilePhone + "%");
		}
		UserContext uc = (UserContext) condition.get(Constants.USER_CONTEXT);
		query.setFirstResult((Integer) condition.get(Constants.KEY_RECORD_FROM));
		query.setMaxResults(uc.getPageSize());
		List<BgUser> list = query.getResultList();

		logger.info("======= " + list);

		return list;
	}

	@Override
	public int getCountByCondition(Map<String, Object> condition) {

		StringBuffer sqlquery = new StringBuffer();
		sqlquery.append("select count(*) from BgUser as a where 1=1 and a.id>2 ");

		String username = null;
		String company = null;
		String mobilePhone = null;
		if (condition.get("username") != null) {
			username = String.valueOf(condition.get("username"));
		}
		if (condition.get("company") != null) {
			company = String.valueOf(condition.get("company"));
		}
		if (condition.get("mobilePhone") != null) {
			mobilePhone = String.valueOf(condition.get("mobilePhone"));
		}
		//
		if (username != null) {
			sqlquery.append(" and a.username like :username");
		}
		if (company != null) {
			sqlquery.append(" and a.company like :company");
		}
		if (mobilePhone != null) {
			sqlquery.append(" and a.mobilePhone like :mobilePhone");
		}

		Query query = em.createQuery(sqlquery.toString());
		if (username != null) {
			query.setParameter("username", "%" + username + "%");
		}
		if (company != null) {
			query.setParameter("company", "%" + company + "%");
		}
		if (mobilePhone != null) {
			query.setParameter("mobilePhone", "%" + mobilePhone + "%");
		}
		Object obj = query.getSingleResult();

		logger.info("======= count=" + obj);

		return Integer.parseInt(obj.toString());
	}

	@SuppressWarnings("unchecked")
	public BgUser findByUsername(String username) {

		logger.info("RepoImpl username=" + username);

		Query query = em.createQuery("from BgUser as au where au.username='"
				+ username + "' ");
		List<BgUser> list = query.getResultList();
		if (list != null && list.size() > 0) {
			return list.get(0);
		}
		return null;
	}

}
